package utils.json;

import com.alibaba.fastjson.JSON;
import entity.ConfigFile;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.nio.charset.StandardCharsets;

/**
 * 读取配置JSON文件，获取各项配置
 */
@Slf4j
@Getter
public class InitConfig {
    private static ConfigFile config;
    private static final InitConfig instance = new InitConfig();
    public static int PERIOD = 60;
    public static int RSSHUB_DELAY = 0;
    public static int WEBDAV_DELAY = 30;
    public static int ARIA2_DELAY = 10;
    public static int PUSH_DELAY = 0;

    private InitConfig() {
        File configFile = new File(System.getProperty("user.dir") + File.separator + "config.json");
        try {
            String s = FileUtils.readFileToString(configFile, StandardCharsets.UTF_8);
            config = JSON.parseObject(s, ConfigFile.class);
            PERIOD = config.getPeriod();
            RSSHUB_DELAY = config.getRssHubDelay();
            WEBDAV_DELAY = config.getWebDavDelay();
            ARIA2_DELAY = config.getAria2Delay();
            PUSH_DELAY = config.getPushDelay();
//            log.info(config.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static InitConfig getInstance() {
        return instance;
    }

    public static boolean isEnableRssHub() {
        return config.getRSSHub() != null && config.getRSSHub().isEnable();
    }

    public static boolean isEnableWebDav() {
        return config.getWebdav() != null && config.getWebdav().isEnable();
    }

    public static boolean isEnableAria2() {
        return config.getAria2() != null && config.getAria2().isEnable();
    }

    public static boolean isEnablePush() {
        return config.getPush() != null && config.getPush().isEnable();
    }

    public ConfigFile getConfig() {
        return config;
    }
}
